#encoding=utf-8
"""封装公共类方法
"""
from Driver import *
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import sys
import os
from Element import Elements


class Function():

    #若在这边init则会初始化两边
    # def __init__(self):
    #     t=AppiumTest()
    #     self.driver=t.get_driver()
    #     print(1)

     #页面左滑方法，num为滑动次数


    def swip_left(self,num,driver):
        x =driver.get_window_size()["width"]
        y =driver.get_window_size()["height"]
        x1 = int(x * 0.75)
        y1 = int(y * 0.5)
        x2 = int(x * 0.05)
        try:
            for i in range(num):
                driver.swipe(x1, y1, x2, y1, 800)
                time.sleep(2)
            print("启屏页滑动结束")
        except:
            print("启屏页滑动异常")






    # 首页上滑方法
    def swip_up(self, num,driver):
        x = driver.get_window_size()["width"]
        y = driver.get_window_size()["height"]
        x1 = int(x * 0.5)
        y1 = int(y * 0.75)
        y2 = int(y * 0.05)
        try:
            for i in range(num):
                driver.swipe(x1, y1, x1, y2, 800)
                time.sleep(1)
            print("首页上滑结束！")
        except:
            print("首页上滑异常")





    #确定按钮，可用于一些系统弹框等,找3遍
    def always_allow(self,driver):
        for i in range(3):
            loc = ("xpath", "//*[@text='确定']")
        try:
            e = WebDriverWait(driver, 1, 0.5).until(EC.presence_of_element_located(loc))
            e.click()
            print("已经点击确定按钮")

        except:
            pass

    # 封装删除操作，找两遍
    def always_del(self,driver):
        for i in range(2):
            loc = ("xpath", "//*[@text='删除']")
        try:
            e = WebDriverWait(driver, 10, 0.5).until(EC.presence_of_all_elements_located(loc))
            e.click()
            print("已经点击删除按钮")
        except:
            pass




    #读取测试数据
    def user_info(self):
        user_file=open('D:/pypronjects/tzzb-Appium/user.txt','r')
        lines=user_file.readlines()

        #遍历测试数据
        for line in lines:
            self.account=line.split(',')[0]
            self.passwd=line.split(',')[1]
            # return line
            return self.account,self.passwd

        user_file.close()


    #银行理财模块的搜索过程
    def search(self,driver):
        e = Elements()
        self.swip_up(2,driver)
        driver1 = e.get_xpath("//*[@class='android.widget.TextView' and @text='添加资产']", driver)
        driver1.click()
        self.swip_up(1,driver)
        e.get_id("com.hexin.zhanghu:id/add_bank_text", driver).click()
        e.get_id("com.hexin.zhanghu:id/navi_right_txt", driver).click()
        e.get_id("com.hexin.zhanghu:id/com_search_cet", driver).send_keys("bj")
        e.get_xpath(
            "//*[@class='android.widget.RelativeLayout' and @instance='2']", driver).click()
        text= e.get_id("com.hexin.zhanghu:id/navi_title", driver).get_attribute("name")
        try:
            self.assertEqual(text, u"北京银行")
            print("搜索成功")
        except:
            pass



    # 截图
    def getScreenShot(self,driver,name):
        time = self.getTime()

        path = 'D:/pypronjects/tzzb-AppiumUI/result/screenshot/%s/'%time
        if not os.path.exists(path):
            os.makedirs(path)
        else:
            pass

        filename=path+'%s.png'%name
        driver.get_screenshot_as_file(filename)

    # 获取时间：年-月-日，
    def getTime(self):
        tamp=time.strftime("%Y-%m-%d")
        return tamp




